package com.lark.web.service.impl;

import java.util.List;

import com.github.dockerjava.api.DockerClient;
import com.github.pagehelper.PageHelper;
import com.lark.common.core.page.PageDomain;
import com.lark.common.utils.StringUtils;
import com.lark.common.utils.sql.SqlUtil;
import com.lark.web.docker.service.DockerUtil;
import com.lark.web.domain.StorageVolume;
import com.lark.web.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.lark.common.core.domain.BaseEntity;
import com.lark.common.core.service.BaseServiceImpl;
import com.lark.web.mapper.NetworkMapper;
import com.lark.web.domain.Network;
import com.lark.web.service.NetworkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 网络管理Service业务层处理
 * @author fei
 * @email zhangfei0210@163.com
 * @date 2024-08-25
 */
@Service
@Transactional(readOnly = true)
public class NetworkServiceImpl extends BaseServiceImpl<NetworkMapper, Network> implements NetworkService {

    private static final Logger log = LoggerFactory.getLogger(NetworkServiceImpl.class);
    @Autowired
    private HostService hostService;
    /**
     * 获取单条数据
     * @param network 网络管理
     * @return 网络管理
     */
    @Override
    public Network get(Network network) {
        Network dto = super.get(network);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 网络管理id
     * @return 网络管理
     */
    @Override
    public Network get(String id) {
        Network dto = super.get(id);
        return dto;
    }

    /**
     * 查询网络管理列表
     * @param network 网络管理
     * @return 网络管理
     */
    @Override
    public List<Network> findList(Network network) {
		List<Network> networkList = super.findList(network);
        return networkList;
    }

    /**
     * 分页查询网络管理列表
     * @param network 网络管理
     * @return 网络管理
     */
    @Override
    public PageInfo<Network> findPage(Network network) {
        DockerClient client = hostService.buildClient(network.getIp());
        List<Network> networkList = DockerUtil.getNetworks(client);

        PageDomain page = network.getPage();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            Boolean reasonable = page.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
        return new PageInfo(networkList);
    }

    /**
     * 保存网络管理
     * @param network
     * @return 结果
     */
    @Override
    public boolean save(Network network) {
        return super.save(network);
    }

    /**
     * 删除网络管理信息
     * @param network
     * @return 结果
     */
    @Override
    public boolean remove(Network network) {
        return super.remove(network);
    }

    /**
     * 批量删除网络管理
     * @param ids 需要删除的网络管理ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteNetworkByIds(String[] ids) {
        return mapper.deleteNetworkByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

}
